from fastapi import APIRouter, Depends, HTTPException, status, Query
from sqlalchemy.orm import Session
from typing import List, Optional

from app.db.database import get_db
from app.core.security import get_current_admin_user
from app.schemas.campsite import Campsite, CampsiteCreate, CampsiteUpdate, CampsiteBrief
from app.crud import campsite as campsite_crud

router = APIRouter()

@router.get("/", response_model=List[CampsiteBrief])
async def list_campsites(
    skip: int = 0,
    limit: int = 100,
    status: Optional[str] = None,
    region: Optional[str] = None,
    search: Optional[str] = None,
    current_admin: dict = Depends(get_current_admin_user),
    db: Session = Depends(get_db)
):
    """
    管理员获取所有营地列表
    可以通过状态、区域、搜索条件过滤
    """
    campsites = campsite_crud.get_campsites(
        db, 
        skip=skip, 
        limit=limit, 
        status=status,
        region=region,
        search=search,
        is_admin=True  # 标记为管理员查询，可以查看未发布的营地
    )
    return campsites

@router.post("/", response_model=Campsite, status_code=status.HTTP_201_CREATED)
async def create_campsite(
    campsite: CampsiteCreate,
    current_admin: dict = Depends(get_current_admin_user),
    db: Session = Depends(get_db)
):
    """管理员创建营地"""
    return campsite_crud.create_campsite(db=db, campsite=campsite)

@router.get("/{campsite_id}", response_model=Campsite)
async def get_campsite(
    campsite_id: int,
    current_admin: dict = Depends(get_current_admin_user),
    db: Session = Depends(get_db)
):
    """管理员获取指定营地详情"""
    db_campsite = campsite_crud.get_campsite(db, campsite_id=campsite_id, is_admin=True)
    if not db_campsite:
        raise HTTPException(status_code=404, detail="营地不存在")
    return db_campsite

@router.put("/{campsite_id}", response_model=Campsite)
async def update_campsite(
    campsite_id: int,
    campsite_update: CampsiteUpdate,
    current_admin: dict = Depends(get_current_admin_user),
    db: Session = Depends(get_db)
):
    """管理员更新营地"""
    updated_campsite = campsite_crud.update_campsite(
        db, 
        campsite_id=campsite_id, 
        campsite_update=campsite_update
    )
    if not updated_campsite:
        raise HTTPException(status_code=404, detail="营地不存在")
    return updated_campsite

@router.delete("/{campsite_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_campsite(
    campsite_id: int,
    current_admin: dict = Depends(get_current_admin_user),
    db: Session = Depends(get_db)
):
    """管理员删除营地"""
    result = campsite_crud.delete_campsite(db, campsite_id=campsite_id)
    if not result:
        raise HTTPException(status_code=404, detail="营地不存在")
    return {"status": "success", "message": "营地删除成功"}

@router.patch("/{campsite_id}/publish", response_model=Campsite)
async def publish_campsite(
    campsite_id: int,
    current_admin: dict = Depends(get_current_admin_user),
    db: Session = Depends(get_db)
):
    """管理员发布营地"""
    updated_campsite = campsite_crud.update_campsite_status(
        db, 
        campsite_id=campsite_id, 
        is_published=True
    )
    if not updated_campsite:
        raise HTTPException(status_code=404, detail="营地不存在")
    return updated_campsite

@router.patch("/{campsite_id}/unpublish", response_model=Campsite)
async def unpublish_campsite(
    campsite_id: int,
    current_admin: dict = Depends(get_current_admin_user),
    db: Session = Depends(get_db)
):
    """管理员取消发布营地"""
    updated_campsite = campsite_crud.update_campsite_status(
        db, 
        campsite_id=campsite_id, 
        is_published=False
    )
    if not updated_campsite:
        raise HTTPException(status_code=404, detail="营地不存在")
    return updated_campsite

@router.post("/{campsite_id}/photos", response_model=dict, status_code=status.HTTP_201_CREATED)
async def add_campsite_photo(
    campsite_id: int,
    photo_url: str,
    current_admin: dict = Depends(get_current_admin_user),
    db: Session = Depends(get_db)
):
    """管理员添加营地照片"""
    result = campsite_crud.add_campsite_photo(
        db, 
        campsite_id=campsite_id, 
        photo_url=photo_url
    )
    if not result:
        raise HTTPException(status_code=404, detail="营地不存在")
    return {"status": "success", "message": "营地照片添加成功"}

@router.delete("/{campsite_id}/photos/{photo_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_campsite_photo(
    campsite_id: int,
    photo_id: int,
    current_admin: dict = Depends(get_current_admin_user),
    db: Session = Depends(get_db)
):
    """管理员删除营地照片"""
    result = campsite_crud.delete_campsite_photo(
        db, 
        campsite_id=campsite_id, 
        photo_id=photo_id
    )
    if not result:
        raise HTTPException(status_code=404, detail="营地照片不存在")
    return {"status": "success", "message": "营地照片删除成功"} 